package com.benz.project.pvm.service;

import com.benz.project.pvm.domain.Version;
import com.benz.project.pvm.repository.VersionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class VersionService {

    @Autowired
    private VersionRepository versionRepository;

    /**
     * 新增版本
     * @param name
     * @param version
     * @param desc
     * @param project
     * @param type
     * @param updateTime
     * @param handler
     */
    public void addVersion(String name,
                           String version,
                           String desc,
                           String project,
                           Integer type,
                           String updateTime,
                           String handler){
        Version versionObj = new Version();
        versionObj.setId(UUID.randomUUID().toString());
        versionObj.setName(name);
        versionObj.setVersion(version);
        versionObj.setDesc(desc);
        versionObj.setProject(project);
        versionObj.setType(type);
        versionObj.setUpdateTime(updateTime);
        versionObj.setHandler(handler);
        versionRepository.save(versionObj);
    }

    /**
     * 修改版本
     * @param id
     * @param name
     * @param version
     * @param desc
     * @param project
     * @param type
     * @param updateTime
     * @param handler
     */
    public void updateVersion(String id,
                              String name,
                              String version,
                              String desc,
                              String project,
                              Integer type,
                              String updateTime,
                              String handler){
        Version versionObj = versionRepository.getOne(id);
        versionObj.setName(name);
        versionObj.setVersion(version);
        versionObj.setDesc(desc);
        versionObj.setProject(project);
        versionObj.setType(type);
        versionObj.setUpdateTime(updateTime);
        versionObj.setHandler(handler);
        versionRepository.save(versionObj);
    }

    /**
     * 删除版本
     * @param ids
     */
    public void deleteVersions(String[] ids){
        for(String id : ids){
            versionRepository.deleteById(id);
        }
    }

    /**
     * 获取单个版本信息
     * @param id
     * @return
     */
    public Version getVersion(String id){
        return versionRepository.getOne(id);
    }

    /**
     * 获取所有版本信息
     * @return
     */
    public List<Version> getVersions(String projectId){
        return versionRepository.findAllByProject( projectId );
    }

}
